package com.wbao.dianniu.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.CanDoBlankGridView;
import com.wbao.dianniu.customView.NoScrollListView;
import com.wbao.dianniu.customView.SpanTextView;
import com.wbao.dianniu.data.Childrens;
import com.wbao.dianniu.data.QuestAnswerListData;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AnswerPraiseManager;
import com.wbao.dianniu.manager.KnowAnswerGoodManger;
import com.wbao.dianniu.update.TwoComDataChangeManager;
import com.wbao.dianniu.utils.ActivityUtils;
import com.wbao.dianniu.utils.AnswerListType;
import com.wbao.dianniu.utils.CommentLongClickHelper;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.ReportType;
import com.wbao.dianniu.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 问题回答列表
 * <p>
 *
 * @author yejianjun
 */
public class QuestAnswerAdapter extends BaseAdapter {
    private Context mContext;
    private List<QuestAnswerListData> mList = new ArrayList<>();
    private int type;

    public QuestAnswerAdapter(Context context, int type) {
        mContext = context;
        this.type = type;
    }

    public void addData(List<QuestAnswerListData> list) {
        for (int i = 0; i < list.size(); i++) {
            mList.add(list.get(i));
        }

    }

    public List<QuestAnswerListData> getList() {
        return mList;
    }

    public void clear() {
        if (null != mList) {
            mList.clear();
        }
    }

    public void deleteOneData(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }

    public void deleteById(int id) {
        if (null == mList)
            return;
        for (int i = 0; i < mList.size(); i++) {
            if (id == mList.get(i).id) {
                mList.remove(i);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public void addOneData(QuestAnswerListData data) {
        if (null != mList) {
            mList.add(0, data);
        }
    }

    public void priseNotify(int id, int isGood, int goodCounts) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).id == id) {
                mList.get(i).isGood = isGood;
                mList.get(i).goodCount = goodCounts;
                notifyDataSetChanged();
                break;
            }
        }
    }

    // 新增一条二级回复
    public void addOneTwoComment(int id, Childrens childrens) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).id == id) {
                if (null == mList.get(i).childrens) {
                    mList.get(i).childrens = new ArrayList<>();
                }
                mList.get(i).childrens.add(0, childrens);
                mList.get(i).childrensCount++;
                notifyDataSetChanged();
                break;
            }
        }

    }

    // 删除一条二级回复
    public void removeOneTwoComment(int id, int childId) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).id == id) {
                if (null == mList.get(i).childrens) {
                    continue;
                }
                for (int j = 0; j < mList.get(i).childrens.size(); j++) {
                    if (mList.get(i).childrens.get(j).id == childId) {
                        mList.get(i).childrens.remove(j);
                        mList.get(i).childrensCount--;
//                        notifyDataSetChanged();
                        break;
                    }
                }
                break;
            }
        }

    }


    @Override
    public int getCount() {
        if (null == mList)
            return 0;
        else
            return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (null == convertView) {
            LayoutInflater mInflater = LayoutInflater.from(mContext);
            convertView = mInflater.inflate(R.layout.quest_answer_list_item, null);
            holder = new Holder();
            holder.headIV = convertView.findViewById(R.id.user_head);
            holder.nameTV = convertView.findViewById(R.id.user_name);
            holder.labelTV = convertView.findViewById(R.id.user_label);
            holder.sexIV = convertView.findViewById(R.id.user_sex);
            holder.partnerIV = convertView.findViewById(R.id.user_partner);
            holder.addV = convertView.findViewById(R.id.add_v_iv);
            holder.goodCounts = convertView.findViewById(R.id.quest_answer_praise_tv);
            holder.sandianIV = convertView.findViewById(R.id.quest_answer_comment_sandian);
            holder.content = convertView.findViewById(R.id.quest_answer_content);
            holder.gridView = convertView.findViewById(R.id.noScroollGridView);
            holder.twoAnswerLayout = convertView.findViewById(R.id.two_comment_layout);
            holder.labelRelative = convertView.findViewById(R.id.user_small_relative);
            holder.listView = convertView.findViewById(R.id.two_comment_listView);
            holder.allCommentTV = convertView.findViewById(R.id.see_all_comment);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        QuestAnswerListData data = mList.get(position);

        PictureDownloadUtils.displayHeadImage(mContext, holder.headIV, data.userInfo.headPic);
        holder.nameTV.setText(data.userInfo.realName == null ? "" : data.userInfo.realName);
        if (null != data.anonymous && 1 == data.anonymous) {
            holder.labelRelative.setVisibility(View.GONE);
            holder.sexIV.setVisibility(View.GONE);
            holder.partnerIV.setVisibility(View.GONE);
        } else {
            holder.labelRelative.setVisibility(View.VISIBLE);
            holder.sexIV.setVisibility(View.VISIBLE);
            holder.partnerIV.setVisibility(View.VISIBLE);
            holder.labelTV.setText(data.userInfo.label == null ? "" : data.userInfo.label);
            Utils.showAuth(data.userInfo, holder.addV);
            if (null == data.userInfo.sex) {
                Utils.showSex(mContext, null, holder.sexIV);
            } else {
                Utils.showSex(mContext, data.userInfo.sex, holder.sexIV);
            }
            Utils.showPartner(mContext, data.userInfo.partner, holder.partnerIV);
        }

        Utils.praiseChanged(mContext, data.isGood, data.goodCount, holder.goodCounts);
        holder.content.setEmojiText(data.content, data.noticeJson);


        // 第一步 解析图片列表数据
        if (TextUtils.isEmpty(data.answerImgs)) {
            holder.gridView.setVisibility(View.GONE); // 没有图片资源就隐藏GridView
        } else {
            holder.gridView.setVisibility(View.VISIBLE);
            Utils.showGridView(mContext, data.answerImgs, holder.gridView, true,true);
        }

        int questId = 0;
        if (type == AnswerListType.TYPE_AM || type == AnswerListType.TYPE_QUEST) {
            questId = data.questId;
        } else if (type == AnswerListType.TYPE_KNOW) {
            questId = data.knowledgeId;
        }

        List<Childrens> childrens = data.childrens;
        //点击事件
        OnClickListener headListener = new clickListener(data);

        // 第二步 解析回复列表数据
        if (null != childrens && childrens.size() > 0) {
            holder.twoAnswerLayout.setVisibility(View.VISIBLE);
            mList.get(position).adapter = new ComReplyAdapter(mContext, childrens, questId, data.id, type, data);
            holder.listView.setAdapter(mList.get(position).adapter);
            if (data.childrensCount > childrens.size()) {
                holder.allCommentTV.setVisibility(View.VISIBLE);
                String s = String.format(mContext.getResources().getString(R.string.see_all_comment), data.childrensCount + "");
                holder.allCommentTV.setText(s);
                holder.allCommentTV.setOnClickListener(headListener);
            } else {
                holder.allCommentTV.setVisibility(View.GONE);
            }
        } else {
            holder.twoAnswerLayout.setVisibility(View.GONE);
        }


        holder.headIV.setOnClickListener(headListener);
        holder.content.setOnClickListener(headListener);
        holder.goodCounts.setOnClickListener(new praiseListener(position, data));
        holder.sandianIV.setOnClickListener(headListener);

        return convertView;
    }


    /**
     * 点击功能
     */
    class clickListener implements OnClickListener {
        private QuestAnswerListData data;

        public clickListener(QuestAnswerListData data) {
            this.data = data;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.user_head:
                case R.id.user_head_layout:
                    if (type == AnswerListType.TYPE_AM && 1 == data.anonymous) {
                    } else {
                        ActivityUtils.intoHeadInfoActivity(data.userInfo.accountId, mContext);
                    }
                    break;
                case R.id.quest_answer_content:
                case R.id.see_all_comment:
                    int questId = 0;

                    if (type == AnswerListType.TYPE_AM || type == AnswerListType.TYPE_QUEST) {
                        questId = data.questId;
                    } else if (type == AnswerListType.TYPE_KNOW) {
                        questId = data.knowledgeId;
                    }

                    // 进入二级回复界面
                    ActivityUtils.intoTwoCommentActivity(mContext, questId, data.id, type, data);
                    break;
                case R.id.quest_answer_comment_sandian:
                    // 弹出复制框等....
                    CommentLongClickHelper commentLongClickHelper = CommentLongClickHelper.getInstance();
                    commentLongClickHelper.commentLongClick(mContext, data, ReportType.TYPE_REPLY, data.userInfo.realName, 1, type);
                    break;
                default:
                    break;
            }

        }

    }

    class praiseListener implements OnClickListener {
        private int position;
        private QuestAnswerListData data;

        public praiseListener(int position, QuestAnswerListData data) {
            this.position = position;
            this.data = data;
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.quest_answer_praise_tv) {
                if (1 == data.isGood) {
                    data.goodCount--;
                    data.isGood = 0;
                } else {
                    data.goodCount++;
                    data.isGood = 1;
                }
//                notifyUI(position, data);

                if (type == AnswerListType.TYPE_KNOW) {
                    KnowAnswerGoodManger manager = new KnowAnswerGoodManger(mContext);
                    manager.knowAnswerGood(GlobalContext.getAccountId(), data.id);
                } else if (type == AnswerListType.TYPE_QUEST || type == AnswerListType.TYPE_AM) {
                    //通知后台更新数据
                    AnswerPraiseManager manager = new AnswerPraiseManager(mContext);
                    manager.answerPraise(GlobalContext.getAccountId(), data.id);
                }

                // 通知界面改变点赞状态
                TwoComDataChangeManager.getInstance().priseCommentNotify(data.id, data.isGood, data.goodCount);

            }

        }

    }

    private void notifyUI(int position, QuestAnswerListData data) {
        if (null != data && null != mList) {
            mList.set(position, data);
        }
        notifyDataSetChanged();
    }

    private class Holder extends UserInfoHolder {
        public TextView goodCounts;
        public ImageView sandianIV;
        public SpanTextView content;
        public CanDoBlankGridView gridView;
        public LinearLayout twoAnswerLayout;
        public NoScrollListView listView;
        public TextView allCommentTV;
    }

}
